package cn.cdutetc.library.service.impl;

import cn.cdutetc.library.entity.enums.BookStatusEnum;
import cn.cdutetc.library.entity.pojo.BookItem;
import cn.cdutetc.library.mapper.BookItemMapper;
import cn.cdutetc.library.service.BookItemService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图书条目服务实现类
 */
@Service
public class BookItemServiceImpl extends ServiceImpl<BookItemMapper, BookItem> implements BookItemService {

    @Autowired
    private BookItemMapper bookItemMapper;

    @Override
    public List<BookItem> getBookItemsByBookId(Integer bookId) {
        LambdaQueryWrapper<BookItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BookItem::getBookId, bookId)
                .orderByAsc(BookItem::getItemId);
        return bookItemMapper.selectList(queryWrapper);
    }

    @Override
    public List<BookItem> getBookItemsByBookIdAndStatus(Integer bookId, String status) {
        LambdaQueryWrapper<BookItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BookItem::getBookId, bookId)
                .eq(BookItem::getStatus, status)
                .orderByAsc(BookItem::getItemId);
        return bookItemMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBookItemStatus(Integer itemId, String status, String remark) {
        BookItem bookItem = bookItemMapper.selectById(itemId);
        if (bookItem == null) {
            return false;
        }
        
        bookItem.setStatus(status);
        if (remark != null && !remark.trim().isEmpty()) {
            bookItem.setRemark(remark);
        }
        
        return bookItemMapper.updateById(bookItem) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateBookItemStatus(List<Integer> itemIds, String status, String remark) {
        if (itemIds == null || itemIds.isEmpty()) {
            return false;
        }
        
        boolean success = true;
        for (Integer itemId : itemIds) {
            if (!updateBookItemStatus(itemId, status, remark)) {
                success = false;
                break;
            }
        }
        return success;
    }

    @Override
    public List<BookItem> getAvailableBookItems(Integer bookId, Integer count) {
        LambdaQueryWrapper<BookItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BookItem::getBookId, bookId)
                .eq(BookItem::getStatus, BookStatusEnum.IN_STOCK.getCode())
                .orderByAsc(BookItem::getItemId)
                .last("LIMIT " + count);
        return bookItemMapper.selectList(queryWrapper);
    }

    @Override
    public Map<String, Integer> getBookItemStatusCount(Integer bookId) {
        List<BookItem> bookItems = getBookItemsByBookId(bookId);
        Map<String, Integer> statusCount = new HashMap<>();
        
        // 初始化所有状态计数为0
        for (BookStatusEnum status : BookStatusEnum.values()) {
            statusCount.put(status.getCode(), 0);
        }
        
        // 统计各状态数量
        for (BookItem item : bookItems) {
            String status = item.getStatus();
            statusCount.put(status, statusCount.getOrDefault(status, 0) + 1);
        }
        
        return statusCount;
    }
}
